-
Notifications
You must be signed in to change notification settings - Fork 19.6k
[OpenVINO backend] Support numpy.prod #21567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @samthakur587, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces support for the "numpy.prod" operation within the Keras OpenVINO backend. Previously, this operation was not implemented, and tests related to it were excluded. With this change, "numpy.prod" can now be used with the OpenVINO backend, expanding its numerical computation capabilities.
Highlights
- Enable "numpy.prod" support: The core change is the implementation of the "prod" function in "keras/src/backend/openvino/numpy.py", allowing the OpenVINO backend to correctly handle product reductions.
- Remove "prod" from test exclusions: Correspondingly, tests for "numpy.prod" (specifically "NumpyDtypeTest::test_prod" and "NumpyOneInputOpsCorrectnessTest::test_prod") have been removed from the "excluded_concrete_tests.txt" file, indicating they are now expected to pass.
- Robust "prod" implementation: The new "prod" implementation handles various scenarios, including type conversions (e.g., boolean to integer for reduction), processing "axis=None" by flattening the input, and managing "dtype" promotion and conversion to OpenVINO types.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds support for numpy.prod
to the OpenVINO backend. The changes look good, but I found a few issues in the implementation of the prod
function related to dtype handling. Specifically, there's an undefined function call that would lead to a runtime error, and incorrect dtype promotion for uint64
inputs. I've provided a single comment with a suggested refactoring that addresses these issues and improves the overall code quality.
keras/src/backend/openvino/numpy.py
Outdated
x = get_ov_output(x) | ||
x_type = x.get_element_type() | ||
|
||
# Convert to appropriate type for reduction | ||
if x_type == Type.boolean: | ||
# Convert boolean to int32 for reduction | ||
x = ov_opset.convert(x, Type.i32).output(0) | ||
|
||
if axis is None: | ||
flatten_shape = ov_opset.constant([-1], Type.i32).output(0) | ||
x = ov_opset.reshape(x, flatten_shape, False).output(0) | ||
axis = 0 | ||
|
||
if isinstance(axis, tuple): | ||
axis = list(axis) | ||
axis = ov_opset.constant(axis, Type.i32).output(0) | ||
|
||
# Compute the product | ||
result = ov_opset.reduce_prod(x, axis, keepdims).output(0) | ||
|
||
# Convert to the specified dtype if provided | ||
if dtype is not None: | ||
ov_dtype = _convert_to_ov_type(dtype) | ||
result = ov_opset.convert(result, ov_dtype).output(0) | ||
else: | ||
# Handle dtype promotion rules | ||
if x_type == Type.boolean: | ||
result = ov_opset.convert(result, Type.i32).output(0) | ||
elif x_type == Type.i64: | ||
result = ov_opset.convert(result, Type.i64).output(0) | ||
elif x_type in (Type.u8, Type.u16, Type.u32): | ||
result = ov_opset.convert(result, Type.u32).output(0) | ||
elif x_type.is_integral(): | ||
result = ov_opset.convert(result, Type.i32).output(0) | ||
|
||
return OpenVINOKerasTensor(result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation has a couple of issues:
_convert_to_ov_type
is not defined, which will cause aNameError
at runtime.- The dtype promotion logic for
uint64
is incorrect, as it gets converted toint32
, which can lead to data loss. - There's a redundant type conversion for boolean inputs, which is inefficient.
I suggest refactoring the function to handle dtype promotion before the reduction. This makes the logic cleaner, more efficient, and corrects the bugs.
x = get_ov_output(x)
# If a specific dtype is requested, cast the input to that dtype.
if dtype is not None:
ov_dtype = OPENVINO_DTYPES[standardize_dtype(dtype)]
x = ov_opset.convert(x, ov_dtype).output(0)
# Otherwise, apply dtype promotion rules before reduction.
else:
x_type = x.get_element_type()
if x_type == Type.boolean:
x = ov_opset.convert(x, Type.i32).output(0)
elif x_type in (Type.i8, Type.i16):
x = ov_opset.convert(x, Type.i32).output(0)
elif x_type in (Type.u8, Type.u16):
x = ov_opset.convert(x, Type.u32).output(0)
if axis is None:
flatten_shape = ov_opset.constant([-1], Type.i32).output(0)
x = ov_opset.reshape(x, flatten_shape, False).output(0)
axis = 0
if isinstance(axis, tuple):
axis = list(axis)
axis = ov_opset.constant(axis, Type.i32).output(0)
# Compute the product
result = ov_opset.reduce_prod(x, axis, keepdims).output(0)
return OpenVINOKerasTensor(result)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21567 +/- ##
=======================================
Coverage 82.75% 82.75%
=======================================
Files 567 567
Lines 56468 56488 +20
Branches 8818 8824 +6
=======================================
+ Hits 46730 46747 +17
- Misses 7577 7579 +2
- Partials 2161 2162 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
in this pr i have added support of numpy.prod to openvino backend
@gbaned could you please review